//
//  SY_Home_TeaLeavingViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/8/15.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Home_TeaLeavingViewController.h"
#import "DateView.h"
#import "ParticipatorListCell.h"
#import "DropTable.h"
#import "HomeparLeaveModel.h"
#import "SY_ColumnChartView.h"
#import "HomeLeaveCell.h"
#import "HomeParLeaveDetailController.h"
#import "SY_ClassesLeaveCell.h"


@interface SY_Home_TeaLeavingViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDataSource,UIPickerViewDelegate,newDropDownDelegate,reloadDataDelegate,DropTableDelgate>
{
    UILabel      *_dateLabel;
    UIImageView  *_dateImage;
    DateView     *_datePicker;
    NSString     *_selectDateString;
    UILabel      *_navTitlelabel;
    UIImageView  *_navImageView;
    UIImageView  *navBackView;
    SY_ColumnChartView *_chartView;
    
    NSInteger    selectClassNum;
    
    UIView       *_todayDefaultView;
    UIView       *_todayBackView;
    UIView       *_classLeavingBackView;
    
//    UIImageView  *_todayEnptyImageView;
//    UIImageView  *_classEnptyImageView;
    
    int lPageNum;
    
    EmptyStateView *emptyView;//空状态
    EmptyStateView *classemptyView;//空状态
    EmptyStateView *noWifiView;//无网络状态
    
    int wifiMark;//1 加载班级 2 加载今日请假 3 加载班级请假
    
    UILabel *noreadLabel;
    NSInteger noreadNum;
    
    DropTable * drop_table;
}

@property (nonatomic, assign) NSInteger type;// 0 :今日请假   1：班级请假列表

@property (nonatomic, strong) NSMutableArray *classNames; //班级名称
@property (nonatomic, strong) NSMutableArray *classesArr; //班级
@property (nonatomic, strong) NSMutableArray *leaderClassesArr; //班主任的班级
@property (nonatomic, strong) NSMutableArray *leaderClassNames;
@property (nonatomic, strong) NSMutableArray *leavingListArr; //当日请假列表
@property (nonatomic, strong) NSMutableArray *chartListArr; //图表数据
@property (nonatomic, strong) NSMutableArray *classLeaveListArr; //图表数据

@property (nonatomic, copy)   NSString   *classid;
@property (nonatomic, copy)   NSString   *selectDateStr;


@property (nonatomic, strong) UITableView *todayLeaveListTabelView;
@property (nonatomic, strong) UITableView *classLeaveListTabelView;
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIButton *dateButton;



@end

@implementation SY_Home_TeaLeavingViewController

- (void)loadView {
    
    [super loadView];
    
    //self.title=@"请假";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 54)];
    [backView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:backView];
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    _selectDateString = [dateFormatter stringFromDate:nowDate];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
    if (self.type == 1) {
        
    }
    
    
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNoReadNum) name:@"refreshNoReadNum" object:nil];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    selectClassNum = 0;
    
    [self loadAllClassData];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"myfriend_empty" andBottomStr:@"今天同学都在"];
    [self.view addSubview:emptyView];
    
    classemptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake(0, (SCREENHEIGH-110)/2-70, SCREENWIDTH, 110) andBackImage:@"myfriend_empty" andBottomStr:@"同学们都活蹦乱跳，没人请假"];
    [self.view addSubview:classemptyView];
    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-SafeAreaTopHeight-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];
}
- (void)changeNoReadNum
{
    if (noreadNum>0) {
        
        noreadNum -- ;
        
        if (noreadNum == 0) {
            
            noreadLabel.hidden = YES;
        }else
        {
            noreadLabel.text = [NSString stringWithFormat:@"%ld",(long)noreadNum];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Methods

- (void)setupUI {
    
   self.navBar.centerImg.hidden = NO;
   navBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
   navBackView.userInteractionEnabled = YES;
   [self.navBar.centerImg  addSubview:navBackView];
   
   NSString *titleStr = [self.classNames objectAtIndex:selectClassNum];
   _navTitlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,100, 44)];
   _navTitlelabel.text = titleStr;
   [self->_navTitlelabel setWidth:[titleStr length]*19 > 152 ? 152 :[titleStr length]*19];
   _navTitlelabel.centerX = navBackView.centerX;
   _navTitlelabel.textAlignment = NSTextAlignmentCenter;
   _navTitlelabel.font = [UIFont boldSystemFontOfSize:18];
   _navTitlelabel.textColor = RGBACOLOR(0, 0, 0, 1);
   [navBackView addSubview:_navTitlelabel];
   
   
   
   _navImageView=[[UIImageView alloc]initWithFrame:CGRectMake(_navTitlelabel.right + 5,17.5,14, 8)];
   _navImageView.image=[UIImage imageNamed:@"down_choose_"];
   _navImageView.userInteractionEnabled = YES;
   [navBackView addSubview:_navImageView];
   
   UIButton *imgBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
   [imgBtn setFrame:CGRectMake(0, 0, 120, 44)];
   [imgBtn addTarget:self action:@selector(selClass:) forControlEvents:(UIControlEventTouchUpInside)];
   [navBackView addSubview:imgBtn];
   
   self.segment = [[UISegmentedControl alloc] initWithItems:@[@"今日请假",@"班级请假"]];
   [self.segment addTarget:self action:@selector(segmentChange:) forControlEvents:(UIControlEventValueChanged)];
   self.segment.size = CGSizeMake(250, 30);
   self.segment.center = CGPointMake(SCREENWIDTH/2,27+SafeAreaTopHeight);
   self.segment.selectedSegmentIndex = 0;
   self.segment.tintColor = RGBCOLOR(57, 173, 255);
   
   UIFont *font = [UIFont systemFontOfSize:14.0f];
   NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                          forKey:NSFontAttributeName];
   [self.segment setTitleTextAttributes:attributes
                               forState:UIControlStateNormal];
   
   [self.view addSubview:self.segment];
   
   noreadLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 5, 20, 20)];
   noreadLabel.backgroundColor =RGBACOLOR(255, 65, 65, 1);
   noreadLabel.clipsToBounds = YES;
   noreadLabel.layer.cornerRadius = 10;
   noreadLabel.textAlignment = NSTextAlignmentCenter;
   noreadLabel.textColor = [UIColor whiteColor];
   noreadLabel.font = [UIFont systemFontOfSize:12];
   noreadLabel.hidden = YES;
   [self.segment addSubview:noreadLabel];
    
}

- (void)setUpView {
    
    _todayDefaultView = [[UIView alloc]initWithFrame:CGRectMake(0, 56+SafeAreaTopHeight, SCREENWIDTH, 39)];
    
    [_todayDefaultView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_todayDefaultView];
    
    UILabel *defautLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 12, 120, 15)];
    [defautLabel setFont:[UIFont systemFontOfSize:16]];
    [defautLabel setTextColor:RGBCOLOR(0, 0, 0)];
    defautLabel.text = @"当日请假统计";
    
    [_todayDefaultView addSubview:defautLabel];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(12, 38.5, SCREENWIDTH, 0.5)];
    [line setBackgroundColor:RGBCOLOR(221,221,221)];
    [_todayDefaultView addSubview:line];
    
    _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 107, 12, 95, 15)];
    [_dateLabel setFont:[UIFont systemFontOfSize:16]];
    _dateLabel.textAlignment = NSTextAlignmentRight;
    [_dateLabel setTextColor:RGBCOLOR(57, 173, 255)];
    _dateLabel.text = [_selectDateString substringWithRange:NSMakeRange(5, 5)];
    
    [_todayDefaultView addSubview:_dateLabel];
    
    _dateImage = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH - 74, 15, 8, 7)];
    [_dateImage setImage:[UIImage imageNamed:@"leave_time_bluetriangle_"]];
    
    [_todayDefaultView addSubview:_dateImage];
    
    _dateButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_dateButton setFrame:CGRectMake(SCREENWIDTH - 130, 12, 130, 60)];
    [_dateButton addTarget:self action:@selector(showDatePicker) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_todayDefaultView addSubview:_dateButton];
}

- (void)creatTodayBackView {
    
    _todayBackView.hidden = NO;
    self.classLeaveListTabelView.hidden = YES;


    _todayBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 66+39+SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH  - 66-39-40 - SafeAreaTopHeight- SafeAreaBottomHeight)];

    [_todayBackView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_todayBackView];
    
    [_todayBackView addSubview:self.todayLeaveListTabelView];
    [self.todayLeaveListTabelView reloadData];
    
    
    _chartView = [[SY_ColumnChartView alloc]initWithFrame:CGRectMake(0, 15, SCREENWIDTH, 143) items:self.chartListArr];
    [_todayBackView addSubview:_chartView];
    
}

- (void)creatClassLeavingBackView {
 
    _todayBackView.hidden = YES;
    self.classLeaveListTabelView.hidden = NO;
    
    [self.view addSubview:self.classLeaveListTabelView];
    [self classLeavingRequest];
    
    
}

- (void)selClass:(UIButton *)sender {
    
    [self configureDrop];

}

#pragma mark -DropDown

- (void)configureDrop{
    
    if (self.type == 0) {
        
        drop_table = [[DropTable alloc] init];
        drop_table.lists = self.classNames;
        drop_table.localtion = localtionTypeMiddle;
        drop_table.delegate = self;
        
    }else {
        
        drop_table = [[DropTable alloc] init];
        drop_table.lists = self.leaderClassNames;
        drop_table.localtion = localtionTypeMiddle;
        drop_table.delegate = self;
    }
    
    
}

#pragma mark -DropTableDelegate
//切换班级
- (void)tableViewCellSelected:(NSInteger)index{
    
    if (self.type == 0) {
        
        selectClassNum = index;
        NSDictionary *classInfo = [self.classesArr objectAtIndex:index];
        self.classid = [classInfo objectForKey:@"classid"];
        _navTitlelabel.text = [self.classNames objectAtIndex:selectClassNum];
        [self todayClassLeavingRequest];
        
    }else if (self.type == 1) {
        
        if (index == 0) {
            self.classid = [SYAccountTool ShareAccount].account.schoolManage == 0?@"0":@"-1";
            _navTitlelabel.text = @"全部";
        }else {
            NSDictionary *classInfo = [self.leaderClassesArr objectAtIndex:index-1];
            self.classid = [classInfo objectForKey:@"classid"];
            _navTitlelabel.text = [classInfo objectForKey:@"classname"];
        }
        [self classLeavingRequest];
        
    }
    [self->_navTitlelabel setWidth:[_navTitlelabel.text length]*19 > 152 ? 152 :[_navTitlelabel.text length]*19];
    
    _navTitlelabel.centerX = 60;
    _navImageView.x = _navTitlelabel.right+5;
}


#pragma mark - SegmentedControl Actions

- (void)segmentChange:(UISegmentedControl *)segment {
    
    self.type = segment.selectedSegmentIndex;
    
    if (self.type == 0) {
        [self setUpView];
        _todayDefaultView.hidden = NO;
        self.classLeaveListTabelView.hidden = YES;
        [classemptyView hideEmptyImage];
        [emptyView showEmptyImage];
        
        self.classid = [[self.classesArr objectAtIndex:0] objectForKey:@"classid"];
        _navTitlelabel.text = [self.classNames objectAtIndex:0];
        [self todayClassLeavingRequest];
        
    }else if ( self.type == 1) {
        
        [classemptyView showEmptyImage];
        [emptyView hideEmptyImage];
        _todayDefaultView.hidden = YES;
        self.classid =[SYAccountTool ShareAccount].account.schoolManage == 0?@"0":@"-1";
        _navTitlelabel.text = @"全部";
        [self creatClassLeavingBackView];
    }

}

- (void)showDatePicker {
    
    _datePicker = [[DateView alloc] initAddSuperView:self.view canceltype:2];
    
    SY_Home_TeaLeavingViewController *weekSelf = self;
    _datePicker.getDate = ^(NSString *dateStr){
        NSMutableString * dateS = [NSMutableString stringWithFormat:@"%@",dateStr];
        NSString * date = [[dateS componentsSeparatedByString:@" "]firstObject];
        
        NSDate *nowDate = [NSDate date];
        NSString * yearString = [[nowDate description] substringToIndex:4];
        //判断是否是今年
        NSString *nowYear = [dateStr substringToIndex:4];
        if ([yearString isEqualToString:nowYear]) {
            weekSelf->_dateLabel.text = [date substringWithRange:NSMakeRange(5, 5)];
            [weekSelf->_dateImage setX: SCREENWIDTH - 84];
        }else {
            weekSelf->_dateLabel.text = date;
            [weekSelf->_dateImage setX: SCREENWIDTH - 114];
        }
        
        weekSelf->_selectDateString = date;
        [weekSelf todayClassLeavingRequest];
        
    };
}

#pragma mark - Request

// 加载班级
- (void)loadAllClassData {
    
    BaseModel *param = [BaseModel param];
    param.method = @"api_classone_search";
    param.pageno = @"1";
    param.pagesize = @"10000";
    param.state = @"1";
    NSString *lastType = [NSString stringWithFormat:@"%d",[SYAccountTool ShareAccount].account.lasttype];
    param.type = lastType;
    
    [Utils getAllClass:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) {  //没有班级
            
            NSString *message;
            if ([lastType isEqualToString: @"1"]) {
                
                message = @"请先创建/加入班级";
            }else if ([lastType isEqualToString:@"2"]){
                
                message = @"请先加入班级";
            }
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            
            alert.tag = 101;
            [alert show];
            
        }else if(arr.count > 0){ //有班级
            
            [self.classesArr addObjectsFromArray:arr];
            
            for (NSDictionary *dic in arr) {
                
                NSString *className = [dic objectForKey:@"classename"];
                
                [self.classNames addObject:className];
                
                if ([[dic objectForKey:@"type"] integerValue] == 1) {
                    
                    [self.leaderClassesArr addObject:dic];
                    NSString *className = [dic objectForKey:@"classename"];
                    
                    [self.leaderClassNames addObject:className];
                }
            }
            
            self.title = self.classNames.firstObject;
            [self.leaderClassNames insertObject:@"全部" atIndex:0];
            self.classid = [[self.classesArr.firstObject objectForKey:@"classid"] stringValue];
            [self setupUI];
            [self setUpView];
            [self todayClassLeavingRequest];
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self->wifiMark = 1;
            self.todayLeaveListTabelView.hidden = YES;
            [self->noWifiView showEmptyImage];
        }
    }];
}

//今日班级请假情况
- (void)todayClassLeavingRequest {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:@"api_leave_byclass" forKey:@"method"];
    [paramDic setObject:USERID forKey:@"userid"];
    [paramDic setObject:self.classid forKey:@"classid"];
    [paramDic setObject:@"1" forKey:@"pageno"];
    [paramDic setObject:@"1000" forKey:@"pagesize"];
    [paramDic setObject:_selectDateString forKey:@"day"];
    
    [Utils get:nil parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        //self.questionNairesModel = [QuestionNairesModel mj_objectWithKeyValues:[[jsonObject objectForKey:@"re_data"] objectAtIndex:0]];
        NSDictionary *dataDic = [jsonObject objectForKey:@"re_data"];
        
        self->noreadNum = [[dataDic objectForKey:@"re_noreadnum"] integerValue];
        
        if(self->noreadNum >0)
        {
            self->noreadLabel.hidden = NO;
            self->noreadLabel.text = [NSString stringWithFormat:@"%ld",self->noreadNum];
            
        }else
        {
            self->noreadLabel.hidden = YES;
        }
        
        if ([dataDic count] > 0) {
            if ([[dataDic objectForKey:@"re_leavelist"] count] > 0) {
                [self->emptyView hideEmptyImage];
                self.leavingListArr = [[NSMutableArray alloc]init];
                self.chartListArr = [[NSMutableArray alloc]initWithArray:[dataDic objectForKey:@"re_count"]];
                
                for (NSDictionary *dic in [dataDic objectForKey:@"re_leavelist"]) {
                    HomeparLeaveModel *model = [HomeparLeaveModel mj_objectWithKeyValues:dic];
                    [self.leavingListArr addObject:model];
                    
                }
                [self->_chartView removeFromSuperview];
                [self creatTodayBackView];
                
            }else {
                
                [self->_todayBackView removeFromSuperview];
                [self->emptyView showEmptyImage];
                
            }
        }else {
            [self->noWifiView hideEmptyImage];
            [self->emptyView showEmptyImage];
            self->_todayBackView.hidden = YES;
        }

    } failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self->wifiMark = 2;
            self->_todayBackView.hidden = YES;
            [self->emptyView hideEmptyImage];
            [self->noWifiView showEmptyImage];
        }
    }];
}

//班级请假情况
- (void)classLeavingRequest {
    
    BaseModel *param = [BaseModel param];
    param.method = @"api_leave_byclass";
    param.pageno = @"1";
    param.pagesize = @"20";
    param.classid = self.classid;
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        self.classLeaveListArr = [[NSMutableArray alloc]init];
        self.classLeaveListTabelView.hidden = NO;
        
        NSDictionary *dic = [jsonObject objectForKey:@"re_data"];
        
        self->noreadNum = [[dic objectForKey:@"re_noreadnum"] integerValue];
        
        if(self->noreadNum >0) {
            
            self->noreadLabel.hidden = NO;
            self->noreadLabel.text = [NSString stringWithFormat:@"%ld",self->noreadNum];
            
        }else {
            self->noreadLabel.hidden = YES;
        }

        if ([dic count] == 0) {
            
            [self->noWifiView hideEmptyImage];
            [self->classemptyView showEmptyImage];
            
        }else {
            
            NSArray *arr = [[jsonObject objectForKey:@"re_data"] objectForKey:@"re_leavelist"];
            if (arr.count==0) {
                
                [self.classLeaveListTabelView reloadData];
                self.classLeaveListTabelView.tableFooterView = [[UIView alloc] init];
                [self.classLeaveListTabelView.mj_header endRefreshing];
                self.classLeaveListTabelView.hidden = YES;

                [self->classemptyView showEmptyImage];
                return;
                
            }
            
            [self->classemptyView hideEmptyImage];
            
            self->lPageNum = 2;
            
            
            for (NSDictionary *dic in arr) {
                HomeparLeaveModel* model=[HomeparLeaveModel mj_objectWithKeyValues:dic];
                [self.classLeaveListArr addObject:model];
            }
        }

        [self.classLeaveListTabelView.mj_footer endRefreshing];
        [self.classLeaveListTabelView reloadData];
        [self.classLeaveListTabelView.mj_header endRefreshing];
    } failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self->wifiMark = 3;
            self.classLeaveListTabelView.hidden = YES;
            [self.classLeaveListTabelView.mj_header endRefreshing];
            [self->classemptyView hideEmptyImage];
            [self->noWifiView showEmptyImage];
        }
        
    }];
}

- (void)requestMoreLeaveData
{
    BaseModel *param = [BaseModel param];
    param.method = @"api_leave_byclass";
    param.pageno = [NSString stringWithFormat:@"%d",lPageNum];;
    param.pagesize = @"20";
    param.classid = self.classid;
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) {
            
            [self.classLeaveListTabelView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }else if (arr.count < 20) {
            
            for (NSDictionary *dic in arr) {
                HomeparLeaveModel* model=[HomeparLeaveModel mj_objectWithKeyValues:dic];
                [self.classLeaveListArr addObject:model];
            }
            
            [self.classLeaveListTabelView reloadData];
            
            [self.classLeaveListTabelView.mj_footer endRefreshingWithNoMoreData];

            
        }else{
            
            self ->lPageNum++;
            
            for (NSDictionary *dic in arr) {
                HomeparLeaveModel* model=[HomeparLeaveModel mj_objectWithKeyValues:dic];
                [self.classLeaveListArr addObject:model];
            }
            
            [self.classLeaveListTabelView reloadData];
            
            [self.classLeaveListTabelView.mj_footer endRefreshing];

        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self.classLeaveListTabelView.hidden = YES;
            [self.classLeaveListTabelView.mj_footer endRefreshing];
            [self->noWifiView showEmptyImage];
        }
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.todayLeaveListTabelView) {
        return [self.leavingListArr count];
    }else {
        return [self.classLeaveListArr count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.todayLeaveListTabelView) {
        ParticipatorListCell * headCell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
        if (headCell == nil) {
            headCell = [[ParticipatorListCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"identifier"];
        }
        
        headCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([[[self.classesArr objectAtIndex:selectClassNum] objectForKey:@"type"] integerValue] == 1) {
            headCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else {
            headCell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        HomeparLeaveModel *model = [self.leavingListArr objectAtIndex:indexPath.row];
        
        StudentsModel *studentModel = [[StudentsModel alloc]init];
        studentModel.username = model.studentname;
        studentModel.logo = model.studentlogo;
        studentModel.logotype = model.studentlogotype;
        
        [headCell creatCell:studentModel];
        
        //公共类里面的下划线
        for (UIView *find_label in headCell.contentView.subviews) {
            if (find_label.tag == 60){
                //[find_label removeFromSuperview];
                [find_label setY:45.5];
                
                if (indexPath.row == self.leavingListArr.count-1) {
                    [find_label setX:0];
                    [find_label setWidth:SCREENWIDTH];
                }
            }else if (find_label.tag == 54) {
                [find_label removeFromSuperview];
            }
        }
        
        
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 230, 15, 200, 16)];
        contentLabel.tag = 54;
        [contentLabel setFont:[UIFont systemFontOfSize:16]];
        contentLabel.textAlignment = NSTextAlignmentRight;
        contentLabel.textColor = RGBCOLOR(59, 59, 59);
        contentLabel.text = model.typename;
        
        [headCell.contentView addSubview:contentLabel];
        
        return headCell;
    }else {
        
        SY_ClassesLeaveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {

            cell = [[SY_ClassesLeaveCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 61.5, SCREENWIDTH, 0.5)];
        line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
        [cell.contentView addSubview:line];
        
        HomeparLeaveModel *leaveModel=[self.classLeaveListArr objectAtIndex:indexPath.row];
        
        [cell creatCell:leaveModel];
        
        
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == self.todayLeaveListTabelView) {
        
        
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 25)];
        [headerView setBackgroundColor:RGBCOLOR(240, 239, 245)];
        
        UIView *topline=[[UIView alloc]initWithFrame:CGRectMake(0, 0.5, SCREENWIDTH, 0.5)];
        topline.backgroundColor=RGBACOLOR(199, 199, 204, 1);
        [headerView addSubview:topline];
        
        UIView *btmline=[[UIView alloc]initWithFrame:CGRectMake(0, 24.5, SCREENWIDTH, 0.5)];
        btmline.backgroundColor=RGBACOLOR(199, 199, 204, 1);
        [headerView addSubview:btmline];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, 100, 25)];
        [titleLabel setTextColor:RGBCOLOR(57, 173, 255)];
        titleLabel.text = @"请假名单";
        [titleLabel setFont:[UIFont systemFontOfSize:14]];
        
        [headerView addSubview:titleLabel];
        
        return headerView;
    }else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == self.todayLeaveListTabelView) {
        return 25;
    }else {
        return 0;
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.type == 1) {
        
        HomeParLeaveDetailController *detailVC = [[HomeParLeaveDetailController alloc]init];
        
        HomeparLeaveDetailModel *model = [self.classLeaveListArr objectAtIndex:indexPath.row];
        
        if (model.isread == NO) {
            
            detailVC.noreadFlag = @"noread";//未读的进入详情给标志
            
        }
        model.isread = YES;
        
        [self.classLeaveListArr replaceObjectAtIndex:indexPath.row withObject:model];
        
        [self.classLeaveListTabelView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        
        detailVC.lmodel = model;
        
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }else {
        if ([[[self.classesArr objectAtIndex:selectClassNum] objectForKey:@"type"] integerValue] == 1) {
            HomeParLeaveDetailController *detailVC = [[HomeParLeaveDetailController alloc]init];
            detailVC.lmodel = [self.leavingListArr objectAtIndex:indexPath.row];
            
            [self.navigationController pushViewController:detailVC animated:YES];
        }
    }
    
}



#pragma mark - Getter

- (UIButton *)dateButton {
    
    if (_dateButton == nil) {
        
        _dateButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_dateButton setFrame:CGRectMake(SCREENWIDTH - 100 , 66+SafeAreaTopHeight, 80, 39)];
        [_dateButton addTarget:self action:@selector(showDatePicker) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    
    return _dateButton;
}

- (UITableView *)todayLeaveListTabelView {
    
    if (_todayLeaveListTabelView == nil) {


        _todayLeaveListTabelView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 175 + 53 - 40, SCREENWIDTH, SCREENHEIGH - 175 - 53)];
        _todayLeaveListTabelView.delegate = self;
        _todayLeaveListTabelView.dataSource = self;
        _todayLeaveListTabelView.rowHeight = 46;
        _todayLeaveListTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _todayLeaveListTabelView.showsVerticalScrollIndicator= NO ;
    }
    
    return _todayLeaveListTabelView;
}

- (UITableView *)classLeaveListTabelView {
    
    if (_classLeaveListTabelView == nil) {
        _classLeaveListTabelView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 56+SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - 56 - 20)];
        _classLeaveListTabelView.delegate = self;
        _classLeaveListTabelView.dataSource = self;
        _classLeaveListTabelView.rowHeight = 62;
        _classLeaveListTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _classLeaveListTabelView.showsVerticalScrollIndicator= NO ;
        _classLeaveListTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self classLeavingRequest];
        }];
        
        _classLeaveListTabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self requestMoreLeaveData];
        }];
    }
    
    return _classLeaveListTabelView;
}

- (NSMutableArray *)classesArr{
    
    if (_classesArr == nil) {
        
        _classesArr = [NSMutableArray array];
        _classNames = [NSMutableArray array];
        _leaderClassesArr = [NSMutableArray array];
        _leaderClassNames = [NSMutableArray array];
    }
    
    return _classesArr;
}
- (void)reloadCurrentData
{
    [noWifiView hideEmptyImage];
    
    if (wifiMark == 1) {
        
        [self loadAllClassData];
        _todayBackView.hidden = NO;
    }else if (wifiMark == 2)
    {
        [self todayClassLeavingRequest];
        _todayBackView.hidden = NO;
    }else
    {
        [self requestMoreLeaveData];
        self.classLeaveListTabelView.hidden = NO;
    }
    
}

@end
